package com.lyc.controller;

import com.lyc.bean.*;
import com.lyc.mvc.ResponseBody;
import com.lyc.service.CourierService;
import com.lyc.service.ExpressService;
import com.lyc.util.DateFormatUtil;
import com.lyc.util.JSONUtil;
import com.lyc.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if (data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        // 1.获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        // 2.获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        // 3.进行查询
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier c : list) {
            String registerTime = DateFormatUtil.format(c.getRegisterTime());
            String loginTime = c.getLoginTime() == null ? "未登录" : DateFormatUtil.format(c.getLoginTime());
            BootStrapTableCourier c1 = new BootStrapTableCourier(c.getId(), c.getUsername(), c.getUserPhone(), c.getIdNumber(), c.getPassword(), c.getExpressNumber(), registerTime, loginTime);
            list2.add(c1);
        }
        List<Map<String, Integer>> console = CourierService.console();
        int total = console.get(0).get("data_size1");
        // 4.将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String idNumber = request.getParameter("idNumber");
        String password = request.getParameter("password");
        Courier c = new Courier(username, userPhone, idNumber, password, "0");
        boolean flag = CourierService.insert(c);
        Message msg = new Message();
        if(flag){
            // 录入成功
            msg.setStatus(0);
            msg.setResult("快递员录入成功!");
        }else{
            // 录入失败
            msg.setStatus(-1);
            msg.setResult("快递员录入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        Courier c = CourierService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (c == null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String idNumber = request.getParameter("idNumber");
        String password = request.getParameter("password");
        Courier newCourier = new Courier();
        newCourier.setUsername(username);
        newCourier.setUserPhone(userPhone);
        newCourier.setIdNumber(idNumber);
        newCourier.setPassword(password);
        boolean flag = CourierService.update(userPhone, newCourier);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        boolean flag = CourierService.delete(userPhone);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
